package com.smartleanx.module.hrm.controller.admin.employee;

import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.customfield.core.service.bo.CustomFieldDataRespBO;
import com.smartleanx.framework.security.core.annotations.PreAuthenticated;
import com.smartleanx.module.bpm.api.v2.dto.BpmProcessTemplatesDTO;
import com.smartleanx.module.bpm.api.v2.dto.StartProcessInstanceDTO;
import com.smartleanx.module.hrm.context.EmployeeContextHolder;
import com.smartleanx.module.hrm.controller.admin.employee.regularizerecord.EmployeeRegularizeRecordRespVO;
import com.smartleanx.module.hrm.controller.admin.employee.regularizerecord.EmployeeRegularizeRecordSaveReqVO;
import com.smartleanx.module.hrm.controller.admin.employee.vo.*;
import com.smartleanx.module.hrm.service.employee.EmployeeRegularizeRecordService;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import com.smartleanx.module.hrm.service.employee.bo.EmployeeFileRespBo;
import com.smartleanx.module.infra.api.file.dto.FileRefRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static com.smartleanx.framework.common.pojo.CommonResult.success;

@Tag(name = "员工PC端 - 员工")
@RestController
@RequestMapping("/hrm/pc-employee")
@Validated
@Slf4j
public class PCEmployeeController {

    @Resource
    private EmployeeService employeeService;

    @PutMapping("/update")
    @Operation(summary = "更新员工")
    @PreAuthenticated
    public CommonResult<Boolean> updateEmployee(@Valid @RequestBody EmployeeSaveReqVO saveReqVO) {
        employeeService.updateEmployee(saveReqVO);
        return success(true);
    }

    @PutMapping("/updateStatus")
    @Operation(summary = "更新员工状态")
    @PreAuthenticated
    public CommonResult<Boolean> updateEmployeeStatus(@Valid @RequestBody EmployeeUpdateStatusReqVO updateStatusReqVO) {
        employeeService.updateEmployeeStatus(updateStatusReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得员工信息")
    @PreAuthenticated
    public CommonResult<List<List<CustomFieldDataRespBO>>> getEmployeeField(@RequestParam("label") Integer label,
                                                                            @RequestParam("labelGroup") Integer labelGroups) throws IllegalAccessException {
        Long id = EmployeeContextHolder.getEmployeeId();
        return success(employeeService.getEmployeeField(id, label, labelGroups));
    }

    @PostMapping("/employeeRegularize")
    @Operation(summary = "员工提交转正申请")
    @PreAuthenticated
    public CommonResult<Boolean> employeeRegularize(@RequestBody @Valid EmployeeRegularizeRecordSaveReqVO regularizeReqVO) {
        regularizeReqVO.setEmployeeId(EmployeeContextHolder.getEmployeeId());
        employeeService.employeeRegularize(regularizeReqVO);
        return success(true);
    }

    /**
     * 【v2】转正提交
     */
    @PostMapping("/employeeRegularizeNew")
    @Operation(summary = "员工提交转正申请")
    @PreAuthenticated
    public CommonResult<Boolean> employeeRegularizeNew(@RequestBody @Valid StartProcessInstanceDTO param) {
        param.setEmployeeId(EmployeeContextHolder.getEmployeeId());
        employeeService.employeeRegularizeNew(param);
        return success(true);
    }

    @GetMapping("/validateRegularize")
    @Operation(summary = "员工端提交转正申请前校验是否已转正")
    @PreAuthenticated
    public CommonResult<EmployeeBasicInfoRespVo> validateRegularize() {
        return success(employeeService.validateRegularize());
    }

    /**
     * 【v2】转正提交前校验
     */
    @GetMapping("/validateRegularizeNew")
    @Operation(summary = "员工端提交转正申请前校验是否已转正")
    @PreAuthenticated
    public CommonResult<BpmProcessTemplatesDTO> validateRegularizeNew(@RequestParam("type") String type) {
        return success(employeeService.validateRegularizeNew(type,null));
    }

    @GetMapping("/validateChangePost")
    @Operation(summary = "员工端提交调动申请前校验是否已有待审批的记录")
    @PreAuthenticated
    public CommonResult<EmployeeBasicInfoRespVo> validateChangePost() {
        return success(employeeService.validateChangePost());
    }

    /**
     * 【v2】调动提交前校验
     */
    @GetMapping("/validateChangePostNew")
    @Operation(summary = "员工端提交调动申请前校验是否已有待审批的记录")
    @PreAuthenticated
    public CommonResult<BpmProcessTemplatesDTO> validateChangePostNew(@RequestParam("type") String type,@RequestParam("id")Integer id) {
        return success(employeeService.validateChangePostNew(type,id));
    }

    @GetMapping("/validateLeave")
    @Operation(summary = "员工提交离职申请前校验员工状态")
    @PreAuthenticated
    public CommonResult<EmployeeBasicInfoRespVo> validateLeave() {
        return success(employeeService.validateLeave());
    }

    /**
     * 【v2】离职提交前校验
     */
    @GetMapping("/validateLeaveNew")
    @Operation(summary = "员工提交离职申请前校验员工状态")
    @PreAuthenticated
    public CommonResult<BpmProcessTemplatesDTO> validateLeaveNew(@RequestParam("type") String type) {
        return success(employeeService.validateLeaveNew(type,null));
    }

    @PostMapping("/employeeChangePost")
    @Operation(summary = "员工端操作调动")
    @PreAuthenticated
    public CommonResult<Boolean> employeeChangePost(@RequestBody @Valid EmployeeChangePostSaveReqVO changePostReqVO) {
        changePostReqVO.setEmployeeId(EmployeeContextHolder.getEmployeeId());
        employeeService.employeeChangePost(changePostReqVO);
        return success(true);
    }

    /**
     * 【v2】调动提交
     */
    @PostMapping("/employeeChangePostNew")
    @Operation(summary = "用户提交调动申请")
    @PreAuthenticated
    public CommonResult<Boolean> employeeChangePostNew(@RequestBody @Valid StartProcessInstanceDTO param) {
        param.setEmployeeId(EmployeeContextHolder.getEmployeeId());
        employeeService.employeeChangePostNew(param);
        return success(true);
    }

    @PostMapping("/employeeLeave")
    @Operation(summary = "用户提交离职申请")
    @PreAuthenticated
    public CommonResult<Boolean> employeeLeave(@RequestBody @Valid EmployeeLeaveReqVO leaveReqVO) {
        leaveReqVO.setEmployeeId(EmployeeContextHolder.getEmployeeId());
        employeeService.employeeLeave(leaveReqVO);
        return success(true);
    }

    /**
     * 【v2】离职提交
     */
    @PostMapping("/employeeLeaveNew")
    @Operation(summary = "用户提交离职申请")
    @PreAuthenticated
    public CommonResult<Boolean> employeeLeaveNew(@RequestBody @Valid StartProcessInstanceDTO param) {
        param.setEmployeeId(EmployeeContextHolder.getEmployeeId());
        employeeService.employeeLeaveNew(param);
        return success(true);
    }


    @GetMapping("/logPage")
    @Operation(summary = "查看员工信息操作记录")
    @PreAuthenticated
    public CommonResult<PageResult<EmployeeLogRespVO>> getEmployeeActionLog(@Valid EmployeeLogReqVO logReqVO) {
        PageResult<EmployeeLogRespVO> logRespVOList = employeeService.getEmployeeActionLog(logReqVO);
        return success(logRespVOList);
    }

    @PutMapping("/addFile")
    @Operation(summary = "添加附件")
    @PreAuthenticated
    public CommonResult<Boolean> addFile(@RequestBody @Valid EmployeeAddFileReqVO addFileReqVO) {
        employeeService.addFile(addFileReqVO);
        return success(true);
    }

    @GetMapping("/getFileListByFileType")
    @Operation(summary = "根据附件类型获取员工的文件列表")
    @PreAuthenticated
    public CommonResult<List<FileRefRespDTO>> getFileListByFileType(@RequestBody @Valid EmployeeAddFileReqVO addFileReqVO) {
        return success(employeeService.getFileListByFileType(addFileReqVO));
    }

    @GetMapping("/getEmployeeRegularize")
    @Operation(summary = "获取员工转正申请信息")
    @Parameter(name = "id", description = "转正记录id", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<EmployeeRegularizeRecordRespVO> getEmployeeRegularize(@RequestParam("id") Long id) {
        return success(employeeService.getEmployeeRegularize(id));
    }

    @GetMapping("/getEmployeeChangePost")
    @Operation(summary = "获取员工调动记录信息")
    @Parameter(name = "id", description = "调动记录id", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<EmployeeChangePostRespVO> getEmployeeChangePost(@RequestParam("id") Long id) {
        return success(employeeService.getEmployeeChangePost(id));
    }

    @GetMapping("/getEmployeeLeave")
    @Operation(summary = "获取员工离职申请信息")
    @Parameter(name = "id", description = "离职记录id", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<EmployeeLeaveRespVO> getEmployeeLeave(@RequestParam("id") Long id) {
        return success(employeeService.getEmployeeLeave(id));
    }

    @GetMapping("/employeeFiles")
    @Operation(summary = "获取员工附件")
    @PreAuthenticated
    public CommonResult<List<EmployeeFileRespBo>> employeeFiles() {
        Long id = EmployeeContextHolder.getEmployeeId();
        return success(employeeService.employeeFiles(id, false));
    }

    @GetMapping("/getEmployeeInfo")
    @Operation(summary = "获取员工基本信息")
    @PreAuthenticated
    public CommonResult<EmployeeBasicInfoRespVo> getEmployeeInfo() {
        Long id = EmployeeContextHolder.getEmployeeId();
        return success(employeeService.getEmployeeInfo(id));
    }

    @PostMapping("/createEmployeeFileRef")
    @Operation(summary = "创建员工附件关联关系")
    @PreAuthenticated
    public CommonResult<Boolean> createEmployeeFileRef(@RequestBody @Valid EmployeeFileReqVo vo) {
        return success(employeeService.createEmployeeFileRef(vo));
    }

    @GetMapping("/getEmployeeList")
    @Operation(summary = "获取员工列表")
    @PreAuthenticated
    public CommonResult<List<EmployeeLeaderRespVo>> getEmployeeList(@RequestParam(value = "name", required = false) String name) {
        return success(employeeService.getEmployeeList(name));
    }

    @GetMapping("/getEmployeeId")
    @Operation(summary = "获取当前登录用户员工id")
    @PreAuthenticated
    public CommonResult<Long> getEmployeeId() {
        return success(employeeService.getEmployeeId());
    }

    @GetMapping("/getEmployeeSeparation")
    @Operation(summary = "获取员工离职信息（员工档案编辑用）")
    @PreAuthenticated
    public CommonResult<EmployeeSeparationRespVo> getEmployeeSeparation(@RequestParam("employeeId") Long employeeId) {
        return success(employeeService.getEmployeeSeparation(employeeId));
    }


    @DeleteMapping("/removeEmployeeFile")
    @Operation(summary = "删除员工文件")
    @PreAuthenticated
    public CommonResult<Boolean> removeEmployeeFile(@RequestParam("id") Collection<Long> ids) {
        return success(employeeService.removeEmployeeFile(ids));
    }


}
